package com.docker.nitsample;

import android.text.TextUtils;

import com.docker.common.common.config.CommonWidgetModel;
import com.docker.common.common.config.ThiredPartConfig;
import com.docker.common.common.utils.cache.CacheUtils;
import com.docker.core.base.BaseApp;
import com.docker.core.di.netmodule.GlobalConfigModule;
import com.docker.core.di.netmodule.HttpRequestHandler;
import com.docker.module_im.init.NimInitManagerCore;
import com.docker.nitsample.di.DaggerAppComponent;

import java.io.Externalizable;
import java.io.IOException;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class NitApp extends BaseApp {
    @Override
    protected void injectApp() {
        DaggerAppComponent.builder()
                .appModule(getAppModule())
                .globalConfigModule(getGlobalConfigModule())
                .httpClientModule(getHttpClientModule())
                .cacheModule(getCacheModule())
                .build()
                .inject(this);
    }

    @Override
    protected GlobalConfigModule getGlobalConfigModule() {
        return GlobalConfigModule.buidler()
                .baseurl(ThiredPartConfig.ServeUrl)
                .globeHttpHandler(new HttpRequestHandler() {
                    @Override
                    public Response onHttpResultResponse(String httpResult, Interceptor.Chain chain, Response response) {
                        if (response.code() == 505) {
                            Response originalResponse = new Response.Builder()
                                    .code(200)
                                    .protocol(Protocol.HTTP_2)
                                    .message("Network is closed by login")
                                    .body(ResponseBody.create(MediaType.parse("text/html; charset=utf-8"),
                                            "{\"errno\":\"0\",\"errmsg\":\"ok\",\"rst\":[]}")) // 返回空页面
                                    .request(chain.request())
                                    .build();
                            return originalResponse;
                        }
                        return response;
                    }

                    @Override
                    public Request onHttpRequestBefore(Interceptor.Chain chain, Request request) {

                        if (CacheUtils.getUser() != null && !TextUtils.isEmpty(CacheUtils.getUser().uid)) {
                            if (request.body() instanceof FormBody) {  // post 请求增加公共参数
                                FormBody.Builder builder = new FormBody.Builder();
                                FormBody body = (FormBody) request.body();
                                for (int i = 0; i < body.size(); i++) {
                                    builder.add(body.name(i), body.value(i));
                                }
                                builder.add("memberid", CacheUtils.getUser().uid);
                                return request.newBuilder().post(builder.build()).build();
                            } else { // get 请求添加公共参数
                                HttpUrl url = request.url();
                                HttpUrl newUrl = url.newBuilder()
                                        .addEncodedQueryParameter("memberid", CacheUtils.getUser().uid)
                                        .build();
                                request = request.newBuilder()
                                        .url(newUrl).build();
                                return request;
                            }

                        } else {
                            return request;
                        }
                    }
                }).build();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        CommonWidgetModel.initrefresh();
        ThiredPartConfig.init(this);
        // 网易云信初始化
        NimInitManagerCore nimInitManagerCore = new NimInitManagerCore();
        nimInitManagerCore.init(this);
    }
}
